package com.hp.shiro.controller;

import java.util.List;

import javax.annotation.security.PermitAll;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hp.shiro.bean.Result;
import com.hp.shiro.model.Department;
import com.hp.shiro.service.DepartmentService;

@RestController
@RequestMapping(value="/dep")
public class DepartmentController {
	
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 初始化部门tree (根据用户的部门编号初始化能够看见的部门tree)
	 * 属性结构
	 * @param code 用户部门编号
	 * @return
	 */
	@GetMapping(value="/findDepartmentTree/{code}")
	@RequiresPermissions(value= "ROLE_BMGL")
	public List<Department> findDepartmentTree(@PathVariable String code){
		
		return departmentService.findDepartmentTree(code);
	}
	
	
	
	/**
	 * 根据部门获取部门下级部门非tree结构
	 * 属性结构
	 * @param code 用户部门编号
	 * @return
	 */
	@GetMapping(value="/findDepartmentByCode/{code}")
	public List<Department> findDepartmentByCode(@PathVariable String code){
		
		return departmentService.findDepartmentByCode(code);
	}
	
	
	/**
	 * 保存管理部门(新增修改通用)
	 * @param code 用户部门编号
	 * @return
	 */
	@PostMapping(value="/saveDepartment",produces="application/json;charset=UTF-8")
	public Result saveDepartment(@RequestBody Department dp){
		return departmentService.saveDepartment(dp);
	}
		
	/**
	 * 根据部门获取部门本部门和上级部门非tree结构
	 * 属性结构
	 * @param code 用户部门编号
	 * @return
	 */
	@GetMapping(value="/findParentsBydepCode/{code}")
	public List<Department> findParentsBydepCode(@PathVariable String code){
		
		return departmentService.findParentsBydepCode(code);
	}
	
}
